﻿var _sendSynchronous = function(paramObj){   
            var url, method, encode,httpRequest,parser;   
            url = paramObj.url;   
            method = paramObj.method;   
            encode = paramObj.encode;   
            if(typeof method == 'undefined')   
                    method = 'GET';   
            if (window.ActiveXObject) {//IE   
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");   
                } else {//FireFox,Chrome,Safari   
                    httpRequest = new XMLHttpRequest();   
                    }   
            httpRequest.open(method, url, false);   
            if(typeof encode != 'undefined')   
                httpRequest.setRequestHeader("Content-Type", "text/html;charset="+encode);   
            httpRequest.send();   
            if (window.ActiveXObject) {//IE   
                parser = new ActiveXObject("Microsoft.XMLDOM");   
                parser.async = false;   
                parser.load(httpRequest.responseXML);   
            } else if (document.implementation.createDocument) {   
                try {//FireFox   
                    parser = document.implementation.createDocument("", "", null);   
                    parser.async = false;   
                    parser=httpRequest.responseText;
                    //parser = (new DOMParser()).parseFromString(httpRequest.responseText, "text/xml");   
                } catch(e) {//Chrome,Safari   
                    var xmlhttp = new window.XMLHttpRequest();   
                    xmlhttp.open("GET", httpRequest.responseText, false);   
                    xmlhttp.send(null);   
                    parser = xmlhttp.responseXML;
                    //parser = xmlhttp.responseXML.documentElement;   
                }   
            }   
            return parser;   
        }
function _nowdate(){
  return _sendSynchronous({url:"/commons/nowdate.jsp",method:"GET",encode:"UTF-8"});
}
function _nowdatetime(){
  return _sendSynchronous({url:"/commons/nowtime.jsp",method:"GET",encode:"UTF-8"});
}

function _lnowdate(){
var d = new Date();
var vYear = d.getFullYear();
var vMon = d.getMonth() + 1;
var vDay = d.getDate();
var str=vYear+"-"+(vMon<10 ? "0" + vMon : vMon)+"-"+(vDay<10 ?  "0"+ vDay : vDay)
return str;
}
function _lnowmonthdate(){
var d = new Date();
var vYear = d.getFullYear();
var vMon = d.getMonth() + 1;
var vDay = 1;
var str=vYear+"-"+(vMon<10 ? "0" + vMon : vMon)+"-"+(vDay<10 ?  "0"+ vDay : vDay)
return str;
}
function _lnowyeardate(){
var d = new Date();
var vYear = d.getFullYear();
var vMon = d.getMonth() + 1;
var vDay = 1;
var str=vYear
return str;
}
/*
function _nowdatetime(){
var d = new Date();
var vYear = d.getFullYear();
var vMon = d.getMonth() + 1;
var vDay = d.getDate();
var vHour= d.getHours();
var vMinute= d.getMinutes();
var vSecond= d.getSeconds();

var str=vYear+"-"+(vMon<10 ? "0" + vMon : vMon)+"-"+(vDay<10 ?  "0"+ vDay : vDay)+" "+(vHour<10 ?  "0"+ vHour : vHour)+":"+(vMinute<10 ?  "0"+ vMinute : vMinute)+":"+(vSecond<10 ?  "0"+ vSecond : vSecond);
return str;
}
*/

function _GetCookieVal(offset)
//获得Cookie解码后的值
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return decodeURIComponent(document.cookie.substring(offset, endstr));
}

function _GetCookie(name)
//获得Cookie的原始值
{
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return _GetCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function _getUserId(){
  
   return _GetCookie('id');
}

function _getUsername(){
   //return 'admin';
   
   return _GetCookie('username');
}
function _isRole(roleid){
   //return 'admin';
   var role=_GetCookie('role');  
   if (role==null) return false;
   role=role.replace("\"","").replace("\"",""); 
   var arrrole=role.split(',');
   return __arraysearch(arrrole,roleid);
}
function _getName(){
   //return 'admin';
   return _GetCookie('name');
}
function _getBranchCompanyId(){
   //return 'admin';
   return _GetCookie('branchCompanyId');
}

function openNewWindows(url){
   window.open(url);
}

function _getFileName(fp){
  var fname="";
  try{
    var arrFile=fp.split("/");

    fname=arrFile[arrFile.length-1];
  }catch(e){return fp;}
  return fname;

}

function __getRoleIdName(roleid){	
        
        for (var one in allRole){
      	          try{

                  	  if (allRole[one].id==roleid) return allRole[one].name;
                    }catch(e){}
                  }
       
        return roleid;
   }
   function __getRoleIdsBySplit(ids,split){
	   if (ids==null||ids=='') return '';
       var splitcodes=ids.split(split);
       var values;
       for (i=0;i<splitcodes.length;i++){
       	   try{
       	   	 if (splitcodes[i]==''||splitcodes[i]==null) continue;
             if (i>0)
               values=values+split+__getRoleIdName(splitcodes[i]);  
             else values=__getRoleIdName(splitcodes[i]);  
          }catch(e){}
         
       }
       return values
  }
  
  /**
 * 数字格式转换成千分位
 *@param{Object}num
 */
function commafy(num) {
	if(!num){
		return '0';
	}
	if ((num + "").trim() == "") {
		return "";
	}
	if (isNaN(num)) {
		return "";
	}
	num = num + "";
	if (/^.*\..*$/.test(num)) {
		var pointIndex = num.lastIndexOf(".");
		var intPart = num.substring(0, pointIndex);
		var pointPart = num.substring(pointIndex + 1, num.length);
		var intPart = intPart + "";
		var re = /(-?\d+)(\d{3})/
		while (re.test(intPart)) {
			intPart = intPart.replace(re, "$1,$2")
		}
		num = intPart + "." + pointPart;
	} else {
		num = num + "";
		var re = /(-?\d+)(\d{3})/
		while (re.test(num)) {
			num = num.replace(re, "$1,$2")
		}
	}
	return num;
}

/**
 * 去掉两端的空格
 * 
 */
function trim(vString){
	return vString.toString().replace(/^s+|s+$/g, '');
}
function __hasUrlAuth(url){	
    if(url==null||url=='') return false;
    if(_getUsername()=='admin') return true;
    for (var one in allResource){
              	try{
              	  if (one==url&&allResource[one].auth==1) return true;
                }catch(e){}
              }
    return false;
}
function __getUrlName(url){	
    if(url==null||url=='') return '';
    for (var one in allResource){
              	try{
              	  if (one==url) return allResource[one].name;
                }catch(e){}
              }
    return '';
}
function __getDayCn(date){
   var i=date.getDay();
   if (i==0) return '日';
   else if (i==1) return '一';
   else if (i==2) return '二';
   else if (i==3) return '三';
   else if (i==4) return '四';
   else if (i==5) return '五';
   else if (i==6) return '六';


}

function traverseTree(node){ 
    var n = {}; 
    n.id = node.id; 
    n.text = node.text; 
    n.parentId = node.attributes.parentId; 
    n.companyNodeType = node.attributes.companyNodeType;     
    n.companyId = node.attributes.companyId;
    n.deptId = node.attributes.deptId;
    n.titleId = node.attributes.titleId;
    n.cvId = node.attributes.cvId;
    n.parentIds = node.attributes.parentIds;
    n.isLeaf = (node.hasChildNodes()?0:1); 
    n.type = node.attributes.type; 
      
    if(node.isLeaf()){ 
          
    } else { 
        var cds = node.childNodes; 
        var arr = []; 
        for (var i=0; i<cds.length; i++) { 
            arr.push(traverseTree(cds[i])); 
        } 
        n.children = arr; 
    } 
      
    return n; 
};


function array2json(arr){
        	var jsonstr="";        	        	
        	for(i=0;i<arr.length;i++){        		        		  		
        		if (i==0) jsonstr="\""+arr[i].name+"\":\""+arr[i].value+"\"";
        		else jsonstr=jsonstr+",\""+arr[i].name+"\":\""+arr[i].value+"\"";        		
          }
          jsonstr="{"+jsonstr+"}";          
          return JSON.parse(jsonstr);          
        }
 function getRecordArray(records,field) {
	         	var result = [];
	         	for(var i = 0; i < records.length; i++) {
	         		result.push(records[i].id);
	          }
	    	   return result;
	      }



Date.prototype.pattern=function(fmt) {           
    var o = {           
    "M+" : this.getMonth()+1, //月份            
    "d+" : this.getDate(), //日            
    "h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时            
    "H+" : this.getHours(), //小时            
    "m+" : this.getMinutes(), //分            
    "s+" : this.getSeconds(), //秒            
    "q+" : Math.floor((this.getMonth()+3)/3), //季度            
    "S" : this.getMilliseconds() //毫秒            
    };           
    var week = {           
    "0" : "/u65e5",           
    "1" : "/u4e00",           
    "2" : "/u4e8c",           
    "3" : "/u4e09",           
    "4" : "/u56db",           
    "5" : "/u4e94",           
    "6" : "/u516d"          
    };           
    if(/(y+)/.test(fmt)){           
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));           
    }           
    if(/(E+)/.test(fmt)){           
        fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[this.getDay()+""]);           
    }           
    for(var k in o){           
        if(new RegExp("("+ k +")").test(fmt)){           
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));           
        }           
    }           
    return fmt;           
};

function formateDate(date){
   if (!date) return "";
   else return date.pattern("yyyy-MM-dd");  
}
function formateDateShort(date){
   if (!date) return "";
   else return date.pattern("yyyy-MM");  
}
function formateDateTime(date){
   if (!date) return "";
   else return date.pattern("yyyy-MM-dd hh:mm:ss");  
}
        